package com.vm.action.mobile;

import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.Page;
import com.vm.dao.mobile.base.IAppDetailDao;
import com.vm.dao.mobile.base.IMobileAppManageDao;
import com.vm.dao.system.IMessageTypeDao;
import com.vm.dao.system.IUpdateCircleDao;
import com.vm.entity.MessageType;
import com.vm.entity.Section;
import com.vm.entity.UpdateCircle;
import com.vm.log4j.CustomizeLog4j;

public class MobileAppManageAction extends ActionSuper implements Preparable {
	
	private static Logger log = CustomizeLog4j.getLogger(MobileAppManageAction.class);
	
	private Page<Section> page;
	private Section section;
	private IMobileAppManageDao mobileAppManageDao;
	private String pageLinkList;
	private Map<String,String> updateCircleMap;
	private Map<String,String> messageTypeMap;
	private IUpdateCircleDao updateCircleDao;
	private IMessageTypeDao  messageTypeDao;
	private IAppDetailDao appDetailDao;
	private Integer circleId;
	private Integer messageTypeId;
	private Integer id;
	
	public void prepare() throws Exception {
		this.updateCircleMap = this.updateCircleDao.getSelectPoMap("select gxId as id,gxName as name from UpdateCircle");
		this.messageTypeMap = this.messageTypeDao.getSelectPoMap("select pxId as id,pxName as name from MessageType");
	}
	
	public String list(){
		this.page = this.mobileAppManageDao.page(this.PAGE_ROW_SIZE, super.getPageNumber());
		for(Section section : page.getList()){
			Integer count = 0; 
			count = this.appDetailDao.getCountBySectionId(section.getBkId());
			section.setCount(count);
		}
		this.pageLinkList =super.getSplitPageLink("/mobile/MobileAppManageAction_list.do?", this.page);
		return SUCCESS;
	}
	
	public String add() throws Exception {
		if(this.section != null){
			section = null;
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		String[] msgArg = new String[] { "MobileAppManageAction_list.do"};
		String msg = null;
		try {
			if(this.circleId != null){
				UpdateCircle updateCircle = this.updateCircleDao.findById(this.circleId);
				this.section.setUpdateCircle(updateCircle);
			}
			if(this.messageTypeId != null){
				MessageType messageType = this.messageTypeDao.findById(this.messageTypeId);
				this.section.setMessageType(messageType);
			}
			this.mobileAppManageDao.save(this.section);
			msg = super.getResourceMessage("insert_section_success", msgArg);
		} catch (Exception e) {
			log.error("UpdateCircleAction save section error  ", e);
			msg = super.getResourceMessage("insert_section_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String edit(){
		section = this.mobileAppManageDao.findById(this.getId());
		return SUCCESS;
	}

	public String update() throws Exception {
		String[] msgArg = new String[] { "MobileAppManageAction_list.do"};
		String msg = null;
		try {
			if(this.circleId != null){
				UpdateCircle updateCircle = this.updateCircleDao.findById(this.circleId);
				this.section.setUpdateCircle(updateCircle);
			}
			if(this.messageTypeId != null){
				MessageType messageType = this.messageTypeDao.findById(this.messageTypeId);
				this.section.setMessageType(messageType);
			}
			this.mobileAppManageDao.update(this.section);
			msg = super.getResourceMessage("update_section_success", msgArg);
		} catch (Exception e) {
			log.error("UpdateCircleAction save section error  ", e);
			msg = super.getResourceMessage("update_section_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	public String delete() {
		String[] msgArg = new String[] { "MobileAppManageAction_list.do"};
		String msg = null;
		try {
			this.mobileAppManageDao.deleteById(this.section.getBkId());
			msg = super.getResourceMessage("delete_section_success", msgArg);
		} catch (Exception e) {
			log.error("UpdateCircleAction delete section error  ", e);
			msg = super.getResourceMessage("delete_section_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	
	public Section getSection() {
		return this.section;
	}
	public void setSection(Section section) {
		this.section = section;
	}

	public IMobileAppManageDao getMobileAppManageDao() {
		return this.mobileAppManageDao;
	}

	public void setMobileAppManageDao(IMobileAppManageDao mobileAppManageDao) {
		this.mobileAppManageDao = mobileAppManageDao;
	}

	public Page<Section> getPage() {
		return this.page;
	}

	public void setPage(Page<Section> page) {
		this.page = page;
	}

	public String getPageLinkList() {
		return this.pageLinkList;
	}

	public void setPageLinkList(String pageLinkList) {
		this.pageLinkList = pageLinkList;
	}


	public Map<String, String> getUpdateCircleMap() {
		return this.updateCircleMap;
	}

	public void setUpdateCircleMap(Map<String, String> updateCircleMap) {
		this.updateCircleMap = updateCircleMap;
	}

	public Map<String, String> getMessageTypeMap() {
		return this.messageTypeMap;
	}

	public void setMessageTypeMap(Map<String, String> messageTypeMap) {
		this.messageTypeMap = messageTypeMap;
	}

	public IUpdateCircleDao getUpdateCircleDao() {
		return this.updateCircleDao;
	}

	public void setUpdateCircleDao(IUpdateCircleDao updateCircleDao) {
		this.updateCircleDao = updateCircleDao;
	}

	public IMessageTypeDao getMessageTypeDao() {
		return this.messageTypeDao;
	}

	public void setMessageTypeDao(IMessageTypeDao messageTypeDao) {
		this.messageTypeDao = messageTypeDao;
	}

	public Integer getCircleId() {
		return this.circleId;
	}

	public void setCircleId(Integer circleId) {
		this.circleId = circleId;
	}

	public Integer getMessageTypeId() {
		return this.messageTypeId;
	}

	public void setMessageTypeId(Integer messageTypeId) {
		this.messageTypeId = messageTypeId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public IAppDetailDao getAppDetailDao() {
		return appDetailDao;
	}

	public void setAppDetailDao(IAppDetailDao appDetailDao) {
		this.appDetailDao = appDetailDao;
	}
	
}
